package com.pzoom.open.HibAnotation.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import org.junit.Test;

import com.pzoom.open.HibAnotation.Anotation.Column;
import com.pzoom.open.HibAnotation.Anotation.Constraint;
import com.pzoom.open.HibAnotation.Anotation.HibInfoTool;
import com.pzoom.open.HibAnotation.Anotation.Table;

public class TestHibernate {
	private String classPath;

	@Test
	@SuppressWarnings("all")
	public void testLoad() {
		classPath = "com.pzoom.open.HibAnotation.Model.User";
		try {
			Class class1 = Class.forName(classPath);
			Annotation[] annotations = class1.getAnnotations();
			for (Annotation annotation : annotations) {
				if (annotation.annotationType() == Table.class) {
					Table table = (Table) class1.getAnnotation(Table.class);
					System.out.println("Connection... \n Create Table "
							+ table.tablename());
					Field[] fields = class1.getDeclaredFields();
					for (Field field : fields) {
						String str = "";
						if (field.getAnnotation(Column.class) != null) {
							Column column = field.getAnnotation(Column.class);
							String columnName = "";
							String type = "";
							int length = 0;
							columnName = column.columnName().equals(
									HibInfoTool.noclolumNameString) ? field
									.getName() : column.columnName();
							type = column.type().equals(
									HibInfoTool.noTypeString) ? field.getType()
									.toString() : column.type();
							length = column.length() == HibInfoTool.noLenght ? 4
									: column.length();
							Constraint constraint = column.constraint();
							str += columnName + " " + type + " " + length;
							if (constraint.primarykey())
								str += " primary key";
							if (constraint.unique())
								str += "unique";
						}
						System.out.println(str);
					}
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
